package com.example.advancecomponents;

import static com.example.advancecomponents.utils.StarUtil.plantList;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager.widget.ViewPager;

import com.example.advancecomponents.adaptors.SimpleImageViewPagerAdapter;
import com.example.advancecomponents.vo.Star;

import java.util.List;

public class IndexPageActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ViewPager simpleViewPager;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_index_page);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        simpleViewPager = findViewById(R.id.simpleViewPager);

        List<Star> stars = plantList();
        simpleViewPager.setAdapter(new SimpleImageViewPagerAdapter(this, stars));

        radioGroup = findViewById(R.id.radioStep);

        for (int i = 0; i < stars.size(); i++) {
            RadioButton child = new RadioButton(this);

            radioGroup.addView(child);

            if (i == 0) {
                child.setChecked(true);
            }
        }

        simpleViewPager.addOnPageChangeListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        simpleViewPager.clearOnPageChangeListeners();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        radioGroup.clearCheck();
        RadioButton child = (RadioButton) radioGroup.getChildAt(position);
        // 选中
        child.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}